﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.VisualBasic;
using PathologyDAL.Modle;
using PathologyDAL.Service;
using static System.Net.Mime.MediaTypeNames;

namespace PathologyBLL.Service
{
    public class ConditionManageServicels
    {
        private readonly Filter filter;

        public ConditionManageServicels(Filter filter)
        {
            this.filter = filter;
        }

        public async Task<List<FilterCondition>> LoadEffictivItems(string fields)
        {
            ConditionServices conditionServices = new ConditionServices(filter);

            return await conditionServices.GetAllConditionByFilterAsync(fields);
        }



        public async Task<string> AddConditionAsync(string Text)
        {
            ConditionServices conditionServices = new ConditionServices(filter);
            var names = Text.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

            if (names.Length == 1)
            {
                int successNumber = await conditionServices.AddAsync(Text.Trim());
                if (successNumber == 1)
                {
                    return $"【{Text}】添加成功";
                }
                else
                {
                    return $"【{Text}】添加失败";
                }
            }
            else
            {
                int successNumber = await conditionServices.AddAsync(names.ToList());
                if (successNumber == names.Length)
                {
                    return $"【{Text}】添加成功";
                }
                else
                {
                    return $"【{Text}】添加失败";
                }
            }
        }




        public async Task<string> CheckExistAsync(string name)
        {
            ConditionServices conditionServices = new ConditionServices(filter);
            var list = name.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList();

            var listexit = await conditionServices.CheckExistAsync(list);
            StringBuilder stringBuilder = new StringBuilder();
            foreach (var condition in listexit)
            {
                stringBuilder.Append(condition.Name + " ");
            }

            return stringBuilder.ToString();
        }

        public async Task<string> UpdateAsync(string Text, FilterCondition filterCondition)
        {
            ConditionServices conditionServices = new ConditionServices(filter);

            int succcessNumber = await conditionServices.UpdateAsync(filterCondition.Id, Text);
            if (succcessNumber == 1)
            {
                return $"修改【{filterCondition.Name}】为【{Text}】成功";
            }
            else
            {
                return $"修改失败";
            }
        }

        public async Task<string> DeleteAsync(int id)
        {
            ConditionServices conditionServices = new ConditionServices(filter);
            int succcessNumber = await conditionServices.DeleteAsync(id);
            if (succcessNumber > 0)
            {
                return "删除成功！";
            }
            else
            {
                return "删除失败";
            }
        }

        public async Task AddTestDataAsync()
        {
            ConditionServices conditionServices = new ConditionServices(filter);
            string baseName = AttributeValueTool.GetDisplayLable<Filter>(filter.ToString());
            List<string> conditions = new List<string>();
            for (int i = 1; i < 501; i++)
            {
                conditions.Add(baseName + i);
            }
            await conditionServices.AddAsync(conditions);
        }

        public async Task<TransferSetContainer> GetTumourDisPlayForTransferSetAsync(
            FilterCondition currentFilter,
            Possibility possibility
        )
        {
            TransferSetContainer transferSetContainer = new TransferSetContainer();
            TumourServices tumourService = new TumourServices();

            //先读取所有的
            transferSetContainer.Right = await tumourService.LoadByConditionAndPossibilityAsync(
                currentFilter,
                possibility
            );
            transferSetContainer.Left = await tumourService.LoadNotByConditionAndPossibilityAsync(
                transferSetContainer.Right
            );

            return transferSetContainer;
        }

        public async Task SaveTransFerSeRigthChangesAsync(
            List<int> rigthTumour,
            FilterCondition currentFilter,
            int selectedIndex
        )
        {
            //右侧的添加

            TumourServices tumourService = new TumourServices();

            if (rigthTumour.Count > 0)
            {
                await tumourService.AddFilterByFiterAndPossibleAsync(
                    rigthTumour,
                    currentFilter,
                    selectedIndex
                );
            }
        }

        public async Task SaveTransFerSeLeftChangesAsync(
            List<int> leftTumour,
            FilterCondition currentFilter,
            int selectedIndex
        )
        {
            TumourServices tumourService = new TumourServices();
            if (leftTumour.Count > 0)
            {
                await tumourService.RemoveFilterByFiterAndPossibleAsync(
                    leftTumour,
                    currentFilter,
                    selectedIndex
                );
            }
        }

        public async Task<FilterCondition> GetFilterByIDAsync(int iD)
        {
            TumourServices tumourService = new TumourServices();
            return await tumourService.GetFilterByIDAsync(iD);
        }

     
    }
}
